package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("checkitem")
@Slf4j
public class CheckItemController {
    @DubboReference
    private CheckItemService checkItemService;

    /**
     * @Author: 位帅帅
     * @Date: 10:54 2021/1/5
     * @Params: [queryPageBean]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 分页查询检查项
     */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = checkItemService.findPage(queryPageBean);
            log.info("[检查项-分页查询]result: {}", pageResult);
            return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
    }

    /**
     * @Author: 位帅帅
     * @Date: 11:43 2021/1/5
     * @Params: [checkItem]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 添加检查项
     */
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @PostMapping("add")
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            log.info("[检查项-添加]param: {}", checkItem);
            checkItemService.add(checkItem);
            return new Result(true, MessageConst.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            log.error("[检查项-添加]e: {}",e.getMessage());
            return new Result(false,e.getMessage());
        }
    }

    /**
     * @Author: 位帅帅
     * @Date: 11:53 2021/1/5
     * @Params: [id]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 删除检查项
     */
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @PostMapping("delete")
    public Result delete(Integer id) {
        try {
            log.info("[检查项-删除]param: {}", id);
            checkItemService.delete(id);
            return new Result(true, MessageConst.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            log.info("[检查项-删除]e: {}",e.getMessage());
            return new Result(false,e.getMessage());
        }
    }

    /**
     * @Author: 位帅帅
     * @Date: 12:00 2021/1/5
     * @Params: [id]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 根据id查询检查项
     */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @GetMapping("findById")
    public Result findById(Integer id) {
        try {
            CheckItem checkItem = checkItemService.findById(id);
            log.info("[检查项-id查询]result: {}", checkItem);
            return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            log.error("[检查项-id查询]e: {}",e.getMessage());
            return new Result(false,e.getMessage());
        }
    }

    /**
     * @Author: 位帅帅
     * @Date: 12:12 2021/1/5
     * @Params: [checkItem]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 编辑检查项
     */
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @PostMapping("edit")
    public Result edit(@RequestBody CheckItem checkItem) {
        try {
            log.info("[检查项-编辑]param: {}", checkItem);
            checkItemService.edit(checkItem);
            return new Result(true, MessageConst.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            log.error("[检查项-编辑]e: {}",e.getMessage());
            return new Result(false,e.getMessage());
        }
    }

    /**
     * @Author: 位帅帅
     * @Date: 15:26 2021/1/5
     * @Params: []
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 查找全部检查项
    */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @GetMapping("findAll")
    public Result findAll() {
        try {
            List<CheckItem> list = checkItemService.findAll();
            log.info("[检查项-查询全部]result: {}",list);
            return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            log.error("[检查项-查询全部]e: {}",e.getMessage());
            return new Result(false,e.getMessage());
        }
    }
}
